package com.haochang.chunkvod.controller.activity.cash;

import java.util.ArrayList;
import java.util.List;

import com.haochang.base.BaseActivity;
import com.haochang.base.BaseListView;
import com.haochang.base.BaseTextView;
import com.haochang.base.EventProxy;
import com.haochang.base.IEventConstant;
import com.haochang.base.OnBaseClickListener;
import com.haochang.chunkvod.R;
import com.haochang.chunkvod.app.common.intent.IntentKey;
import com.haochang.chunkvod.app.utils.CollectionUtils;
import com.haochang.chunkvod.app.utils.SaleUtils;
import com.haochang.chunkvod.app.utils.ToastUtils;
import com.haochang.chunkvod.controller.adapter.cash.NonOperatingRevenueAdapter;
import com.haochang.chunkvod.model.cash.CashData;
import com.haochang.chunkvod.model.cash.GoodInfo;
import com.haochang.chunkvod.model.cash.NonOperateEnum;
import com.haochang.chunkvod.tools.vod.event.EventObserver;
import com.haochang.chunkvod.view.vod.dialog.KeyBoardDialog;
import com.haochang.chunkvod.view.vod.popupWindow.VodPopWindow;
import com.haochang.chunkvod.view.vod.tab.TabSwitcherTwo;
import com.haochang.chunkvod.view.vod.title.TitleView;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

/**
 *@author qintao
 *created at 2016/7/26 11:55
 * 营业外收入
 */

public class NonOperatingRevenueActivity extends BaseActivity implements KeyBoardDialog.KeyBoardOnSureListener, EventObserver
{
	private TitleView titleView;
	private TabSwitcherTwo tabSwitcherTwo;
	private BaseTextView tv_titleName;
	private BaseListView listView;
	private NonOperatingRevenueAdapter adapter;
	private BaseTextView tv_total, tv_totalPrice;
	private Button btn_pend, btn_checkOut;
	private ArrayList<GoodInfo> goodsInfoList1 = new ArrayList<GoodInfo>();
	private ArrayList<GoodInfo> goodsInfoList2 = new ArrayList<GoodInfo>();
	private CashData cashData;
	private int totalPrice;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		EventProxy.addEventListener(this, IEventConstant.NON_OPERNATE_GOODS);
	}

	@Override
	public void receiveParam()
	{

	}

	@Override
	public void initData()
	{
		cashData = new CashData(NonOperatingRevenueActivity.this);
	}

	@Override
	public void initViews()
	{
		setContentView(R.layout.activity_non_operating_revenue);
		titleView = (TitleView) findViewById(R.id.titleView);
		titleView.setTitle(R.string.nonOperatingRevenue).setTitleOnClickListener(new TitleView.ITitleOnClickListener()
		{
			@Override
			public void onBackClick()
			{
				finish();
			}

			@Override
			public void onDoneClick()
			{
				List<String> list = new ArrayList<String>();
				list.add("退出登录");
				int location[] = new int[2];
				ImageView img = (ImageView) titleView.findViewById(R.id.img_operates);
				img.getLocationOnScreen(location);
				int x = location[0];
				final VodPopWindow pop = new VodPopWindow(NonOperatingRevenueActivity.this, list);
				pop.showPopupWindow(titleView, x);
				pop.setPopWindowListListener(new VodPopWindow.OnPopWindowListListener()
				{
					@Override
					public void onSelectItem(int position)
					{
						switch (position)
						{
						case 0:
							loginOut();
							break;
						default:
							break;
						}
					}
				});
			}
		});
		tv_titleName = (BaseTextView) findViewById(R.id.tv_titleName);
		tabSwitcherTwo = (TabSwitcherTwo) findViewById(R.id.tabSwitcherTwo);
		tabSwitcherTwo.addTab("营业外商品");
		tabSwitcherTwo.addTab("赔偿清单");
		tabSwitcherTwo.setOnTabChangedListener(new TabSwitcherTwo.OnTabChangedListener()
		{
			@Override
			public void onTabChanged(int last, int current)
			{
				tabSwitcherTwo.setSelectedTable(current);
				if (current == 0)
				{
					tv_titleName.setText("营业外商品名");
					adapter.activityEnum = NonOperateEnum.NON_OPERATE;
					tv_total.setText("总计：");
					tv_totalPrice.setBackgroundDrawable(getResources().getDrawable(R.drawable.view_tv_no_bold_bg));
					tv_totalPrice.setClickable(false);
					btn_pend.setVisibility(View.VISIBLE);
					getNonOperatingRevenueGoodsData();
				}
				if (current == 1)
				{
					tv_titleName.setText("赔偿物件名");
					adapter.activityEnum = NonOperateEnum.PAY;
					tv_total.setText("总价值：");
					tv_totalPrice.setBackgroundDrawable(getResources().getDrawable(R.drawable.view_tv_bold_bg));
					tv_totalPrice.setClickable(true);
					tv_totalPrice.setOnClickListener(mOnBaseClickListener);
					btn_pend.setVisibility(View.GONE);
					getPayForGoodsData();
				}
			}
		});
		listView = (BaseListView) findViewById(R.id.listView);
		adapter = new NonOperatingRevenueAdapter(NonOperatingRevenueActivity.this, R.layout.item_non_operate_revenue_list_layout, goodsInfoList1);
		listView.setAdapter(adapter);
		btn_pend = (Button) findViewById(R.id.btn_pend);
		btn_checkOut = (Button) findViewById(R.id.btn_checkOut);
		tv_total = (BaseTextView) findViewById(R.id.tv_total);
		tv_totalPrice = (BaseTextView) findViewById(R.id.tv_totalPrice);
		btn_checkOut.setOnClickListener(mOnBaseClickListener);
		btn_pend.setOnClickListener(mOnBaseClickListener);
		getNonOperatingRevenueGoodsData();
	}

	/**
	 * 获取营业外商品数据
	 */
	private void getNonOperatingRevenueGoodsData()
	{
		cashData.requestNonOperatingRevenueGoods(new CashData.INonOperatingRevenueListener()
		{
			@Override
			public void onRequestSuccess(List<GoodInfo> list)
			{
				goodsInfoList1.clear();
				if (!CollectionUtils.isEmpty(list))
				{
					goodsInfoList1.addAll(list);
					adapter.replaceAll(goodsInfoList1);
				}
			}
		});
		adapter.replaceAll(goodsInfoList1);
	}

	/**
	 * 获取赔偿清单数据
	 */
	private void getPayForGoodsData()
	{
		cashData.requestPayForGoods(new CashData.INonOperatingRevenueListener()
		{
			@Override
			public void onRequestSuccess(List<GoodInfo> list)
			{
				goodsInfoList2.clear();
				if (!CollectionUtils.isEmpty(list))
				{
					goodsInfoList2.addAll(list);
					adapter.replaceAll(goodsInfoList2);
				}
			}
		});
		adapter.replaceAll(goodsInfoList2);
	}

	private OnBaseClickListener mOnBaseClickListener = new OnBaseClickListener()
	{
		@Override
		public void onBaseClick(View v)
		{
			switch (v.getId())
			{
			case R.id.tv_totalPrice:
				KeyBoardDialog keyBoardDialog = new KeyBoardDialog(NonOperatingRevenueActivity.this);
				keyBoardDialog.setKeyBoardOnSureListener(NonOperatingRevenueActivity.this);
				keyBoardDialog.show();
				break;
			case R.id.btn_checkOut:
				Intent intent = new Intent(NonOperatingRevenueActivity.this, SettlementActivity.class);
				intent.putExtra(IntentKey.PRICEVALUE, totalPrice);
				intent.putParcelableArrayListExtra(IntentKey.GOODS_LIST, goodsInfoList2);
				intent.putExtra(IntentKey.ACTIVITY_ENUM, adapter.activityEnum);
				startActivity(intent);
				break;
			case R.id.btn_pend:
				Intent intent2 = new Intent(NonOperatingRevenueActivity.this, PendOperateDetailsActivity.class);
				startActivity(intent2);
				break;
			default:
				break;
			}
		}
	};

	@Override
	public void setValue(String value)
	{

		if (!TextUtils.isEmpty(value))
		{
			if (value.contains("."))
				ToastUtils.showText(NonOperatingRevenueActivity.this, R.string.error_number);
			else if (value.length() > 9)
				ToastUtils.showText(NonOperatingRevenueActivity.this, R.string.error_max_number);
			else
			{
				totalPrice = SaleUtils.getSaleInt(value);
				tv_totalPrice.setText(SaleUtils.getSaleString(totalPrice));
			}
		}
	}

	@Override
	public void onNotify(Object sender, int eventId, Object... args)
	{
		switch (eventId)
		{
		case IEventConstant.NON_OPERNATE_GOODS:
			int total = 0;
			for (GoodInfo goodInfo : adapter.activityEnum == NonOperateEnum.PAY ? goodsInfoList2 : goodsInfoList1)
			{
				total += goodInfo.getPrice() * goodInfo.getOrderNum();
			}
			//			total = total % 100 > 0 ? total / 100 * 100 + 100 : total;//总金额向上取整
			totalPrice = total;
			tv_totalPrice.setText(SaleUtils.getSaleString(totalPrice));
			break;
		default:
			break;
		}
	}

	@Override
	protected void onDestroy()
	{
		EventProxy.removeEventListener(IEventConstant.NON_OPERNATE_GOODS, this);
		super.onDestroy();
	}
}
